#include "ThreadPool.h"
#include <iostream>

ThreadPool::ThreadPool() {}

void ThreadPool::setMode(PoolMode mode) { this->poolMode_ = mode; }

void ThreadPool::setTaskQueMaxThreshold(size_t threshold) {
  this->taskQueThreshHold_ = threshold;
}

// 任务生产者
void ThreadPool::submitTask(std::shared_ptr<Task> Tasksptr) {
  // 获取锁
  std::unique_lock<std::mutex> lock(taskQueMtx_);
  // 线程通信
  notFull_.wait(lock, [&]() { return taskQue_.size() < taskQueThreshHold_; });

  // 队列有空, 任务放入队列
  taskQue_.emplace(Tasksptr);
  taskSize_++;
  // 队列不空了, 通知消费者
  notEmpty_.notify_one();
};

void ThreadPool::start(size_t initThreadSize) {
  this->initThreadSize_ = initThreadSize;

  for (int i = 0; i < initThreadSize_; i++) {
    // threads_.emplace_back(std::make_unique<Thread>([this] {
    //   threadFunc();
    // }));
    auto t_ptr = std::make_unique<Thread>([this] {
      threadFunc();
    });
    threads_.emplace_back(std::move(t_ptr));
  }

  for (int i = 0; i < initThreadSize_; i++) {
    threads_[i]->start();
  }
}

void ThreadPool::threadFunc() {
  std::cout << "begin threadFunc" << "\n";
  std::cout << std::this_thread::get_id() << "\n";
  std::cout << "end threadFunc" << "\n";
}
